package com.lanswon.qzsmk.util.inventoryUtil;

import java.awt.Color;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import org.slf4j.LoggerFactory;

import com.itextpdf.text.BaseColor;
import com.lanswon.qzsmk.exception.SystemException;
import com.lanswon.qzsmk.util.ReadproUtil;
import com.lanswon.qzsmk.vo.TableVo;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.PageSize;
import com.lowagie.text.Phrase;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;

import ch.qos.logback.classic.Logger;

public class CreatePDFUtil {
	
	private static Logger logger = (Logger) LoggerFactory.getLogger(CreatePDFUtil.class);
	//private final static Map<String, Object> map=ReadproUtil.readPro();
	
	public static  void createDocument(PdfPTable table,String param) throws Exception, Exception{
		logger.debug("table==param==>"+param);
		// 创建Document对象(页面的大小为A4,左、右、上、下的页边距为10)
        Document document = new Document(PageSize.A4, 10, 10, 10, 10);
        String[] params = param.split("#");  
        // 建立书写器
        String path = ReadproUtil.CREDENCIAL_PATH + File.separator + params[5]+"_print.pdf";
        System.out.println("path====>"+path);
        PdfWriter.getInstance(document, new FileOutputStream(path));
        // 设置相关的参数
           
        CreatePDFUtil.setParameters(document, params[0], params[1],params[2],params[3],params[4]);
        // 打开文档
        document.open();
        // 添加表格
        document.add(table);
        // 关闭文档
        document.close();
	}
	
    public static PdfPTable setTable(TableVo tableVo) throws com.lowagie.text.DocumentException, IOException{
        //创建一个有x列的表格
    	String[] headers = tableVo.getTableHeader().split("#");
    	Integer tdCount = headers.length;
    	
    	PdfPTable table = new PdfPTable(tdCount);
        // 创建标题
    	table.getDefaultCell().setColspan(tdCount);
    	table.getDefaultCell().setBackgroundColor(Color.lightGray);
        table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
        String titleContent = tableVo.getTableTitle();
        table.addCell(new Phrase(titleContent,CreatePDFUtil.setFont()));
    	// 创建表头和底部
    	for(int count=0;count<2;count++){
    		 for(int i=0;i<headers.length;i++){
    	        	PdfPCell cellObj = new PdfPCell();
    	        	cellObj = CreatePDFUtil.setTableHeader(headers[i]);
    	        	table.addCell(cellObj); 	
    	        }
    	}
       
    	table.setHeaderRows(3);
        table.setFooterRows(1);

        List<String> tableContentList = tableVo.getTableContent();  
        for (int i = 0; i < tableContentList.size(); i++) {
        	String[] contents = tableContentList.get(i).split("#");
            for(int j=0;j<contents.length;j++){
            	PdfPCell cellObj = new PdfPCell();
            	cellObj = CreatePDFUtil.setTableContent(contents[j]);
            	table.addCell(cellObj); 
            }
        }
        
        return table;
        
    }
    /**
     * 设置字体编码格式
     * @return
     * @throws IOException 
     * @throws com.lowagie.text.DocumentException 
     */
    public static Font setFont() {
        BaseFont baseFont = null;
        try {
			baseFont = BaseFont.createFont(CreatePDFUtil.class.getResource("/") +
				       "simsun.ttc,1",BaseFont.IDENTITY_H,BaseFont.EMBEDDED);
		} catch (DocumentException e)
		{
			e.printStackTrace();
			String msg="系统创建字体异常";
			logger.error(msg);
			throw new SystemException(SystemException.DocumentException, msg);

		} catch (IOException e) {
			e.printStackTrace();
			String msg="系统创建字体异常";
			logger.error(msg);
			throw new SystemException(SystemException.IOException, msg);

		} 
        Font font = new Font(baseFont,8, Font.NORMAL,Color.black);
        return font;
    }
    /**
     * 设置cell
     * @param name
     * @return
     * @throws IOException 
     * @throws com.lowagie.text.DocumentException 
     */
    public static PdfPCell setTableHeader(String name) throws com.lowagie.text.DocumentException, IOException{
        
    	PdfPCell cell = new PdfPCell(new Phrase(name,CreatePDFUtil.setFont()));
        //单元格水平对齐方式
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        //单元格垂直对齐方式
        cell.setVerticalAlignment(Element.ALIGN_CENTER);
        cell.setBackgroundColor(Color.LIGHT_GRAY);
        return cell;
    }
    
 public static PdfPCell setTableContent(String name) throws com.lowagie.text.DocumentException, IOException{
        
    	PdfPCell cell = new PdfPCell(new Phrase(name,CreatePDFUtil.setFont()));
        //单元格水平对齐方式
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        //单元格垂直对齐方式
        cell.setVerticalAlignment(Element.ALIGN_CENTER);
 //     cell.setBackgroundColor(Color.);
        return cell;
    }
    
    /**
     * 设置相关参数
     * @param document
     * @return
     */
    public static Document setParameters(Document document,String title,String subject,String keywords,String author,
            String creator){
        // 设置标题
        document.addTitle(title);
        // 设置主题
        document.addSubject(subject);
        // 设置关键字
        document.addKeywords(keywords);
        // 设置作者
        document.addAuthor(author);
        // 设置创建者
        document.addCreator(creator);
        // 设置生产者
        document.addProducer();
        // 设置创建日期
        document.addCreationDate();
        return document;
    }

}
